package org.gongliang.jsmile.common.base;

import java.util.List;
import java.util.Map;

import org.gongliang.jsmile.common.enums.KException;
import org.gongliang.jsmile.common.kit.BeanKit;

import com.google.common.collect.Maps;

/**
 * @description 返回前台的json数据
 * @author 龚亮
 * @date 2015-05-18 14:42:28
 */
public class JSmileResult implements java.io.Serializable {

	private static final long serialVersionUID = 1L;

	public static final String STATE_SUCCESS = "success";
	public static final String STATE_ERROR = "error";

	public static JSmileResult success() {
		return new JSmileResult(KException.SUCCESS.getResCode(),KException.SUCCESS.getResMsg(),STATE_SUCCESS);
	}
	public static JSmileResult error() {
		return new JSmileResult(KException.ERROR.getResCode(),KException.ERROR.getResMsg(),STATE_ERROR);
	}
	public static JSmileResult success(String resMsg) {
		return new JSmileResult(KException.SUCCESS.getResCode(),resMsg,STATE_SUCCESS);
	}
	public static JSmileResult error(String resMsg) {
		return new JSmileResult(KException.ERROR.getResCode(),resMsg,STATE_ERROR);
	}
	public static JSmileResult error(KException e) {
		return new JSmileResult(e.getResCode(),e.getResMsg(),STATE_ERROR);
	}

	/**
	 * ok: 返回成功 error： 返回失败
	 */
	private String resState;
	/**
	 * 错误信息
	 */
	private String resMsg;
	/**
	 * 错误码
	 */
	private Integer resCode;
	/*
	 * 返回的结果格式 {total:0L,rows[]}
	 */
	private Map<String, Object> resData = Maps.newHashMap();
	/*
	 * 附带额外的参数
	 */
	private Map<String, Object> resExtra = Maps.newHashMap();

	public JSmileResult() {
		this.resState = JSmileResult.STATE_SUCCESS;
		this.resCode = KException.SUCCESS.getResCode();
		this.resMsg = KException.SUCCESS.getResMsg();
	}

	public JSmileResult(int resCode, String resMsg, String resState) {
		this.resCode = resCode;
		this.resMsg = resMsg;
		this.resState = resState;
	}

	public void setResData(List<?> rows, Object total) {
		resData.put("total", total);
		resData.put("rows", rows);
	}

	public void setResData(List<?> rows) {
		resData.put("total", rows.size());
		resData.put("rows", rows);
	}

	public void setTotal(Long total) {
		resData.put("total", total);
	}

	public void setRows(List<?> rows) {
		resData.put("rows", rows);
	}

	public String getResMsg() {
		return resMsg;
	}

	public void setResMsg(String resMsg) {
		this.resMsg = resMsg;
	}

	public Integer getResCode() {
		return resCode;
	}

	public void setResCode(Integer resCode) {
		this.resCode = resCode;
	}

	public Map<String, Object> getResExtra() {
		return resExtra;
	}

	public void setResExtra(Map<String, Object> resExtra) {
		this.resExtra = resExtra;
	}

	public void setResExtraObj(Object resExtra) {
		this.resExtra = BeanKit.beanToMap(resExtra);
	}

	public void putExtra(String key, Object value) {
		resExtra.put(key, value);
	}

	public Map<String, Object> getResData() {
		return resData;
	}

	public String getResState() {
		return resState;
	}

	public void setResState(String resState) {
		this.resState = resState;
	}

}
